downcast-rs
Rust enums are great for types where all variations are known beforehand. But a container of user-defined types requires an open-ended type like a trait object. Some applications may want to cast these trait objects back to the original concrete types to access additional functionality and performant inlined implementations.
downcast-rs
adds this downcasting support to trait objects using only safe
Rust. It supports type parameters, associated types, and constraints.
Usage
Add the following to your Cargo.toml
:
[]
= "1.2.1"
This crate is no_std
compatible. To use it without std
:
[]
= { = "1.2.0", = false }
To make a trait downcastable, make it extend either downcast::Downcast
or
downcast::DowncastSync
and invoke impl_downcast!
on it as in the examples
below.
Since 1.2.0, the minimum supported Rust version is 1.36 due to needing stable access to alloc.
impl_downcast!;
// Also supports downcasting `Arc`-ed trait objects by extending `DowncastSync`
// and starting `impl_downcast!` with `sync`.
impl_downcast!;
// With type parameters.
impl_downcast!;
// With associated types.
impl_downcast!;
// With constraints on types.
impl_downcast!;
// With concrete types.
impl_downcast!;
impl_downcast!;
Example without generics
// Import macro via `macro_use` pre-1.30.
extern crate downcast_rs;
use DowncastSync;
// To create a trait with downcasting methods, extend `Downcast` or `DowncastSync`
// and run `impl_downcast!()` on the trait.
impl_downcast!; // `sync` => also produce `Arc` downcasts.
// Concrete types implementing Base.
;
;
Example with a generic trait with associated types and constraints
// Can call macro via namespace since rust 1.30.
extern crate downcast_rs;
use Downcast;
// To create a trait with downcasting methods, extend `Downcast` or `DowncastSync`
// and run `impl_downcast!()` on the trait.
impl_downcast!;
// or: impl_downcast!(concrete Base<u32> assoc H=f32)
// Concrete types implementing Base.
;
;
Why no changes in a while?
This library is a thoroughly-tested boilerplate generator, is code complete, has no unsafe, and is vanishingly unlikely to have any security issues to patch.
License
Copyright 2020, Ashish Myles (maintainer) and contributors. This software is dual-licensed under the MIT and Apache 2.0 licenses.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.